<?php
class sms_util_text{
	static function filter_html($str){
		
		//$str = strip_tags(strip_scripts($str));		
		$str = strip_scripts($str);		
		
//		$regex4 = '`^[\t ]*$`m';
//		$str = preg_replace($regex4, '', $str);
//		
//		$regex5 = '`[\n]{3,}`';
//		$str = preg_replace($regex, "\n\n", $str);
		
		return $str;
	}
	
	static function formatSystemRows(&$rows){
		$regex = "`\[\:([a-z0-9]+)(?:\|[a-z0-9]+)?\:\]`";
		foreach($rows as &$row){
			$sender_name = self::parseSenderName($row['sender_name']);
			if($sender_name){
				$row['sender_name'] = $sender_name;
				$row['sender_name_cn'] = $_SERVER['LOCALE']['apps'][$sender_name][0];
				$target = unserialize($row['target']);
				if(!is_array($target)) {
					continue;
				}
				$links = array();
				
				$a = &$_SERVER['LOCALE']['apps'][$sender_name];
				
				if(!key_exists('return', $target) and $a[1]['addon'][$target['type']]){
					foreach($a[1]['addon'][$target['type']] as $i=>$link){
						$link = eval_template($link, $target);
						array_push($links, $link);
					}
					$row['related_links'] = implode(' <span class="aert-gapline">|</span> ', $links);
				}else{
					$row['return'] = $target['return'];
				}
			}			
		}
	}
	
	static function parseSenderName($str){
		$regex = "`\[\:([a-z0-9]+)(?:\|[a-z0-9]+)?\:\]`";
		$flag = preg_match($regex, $str, $match);
		if($flag){
			return $match[1];
		}else{
			return NULL;
		}
	}
	
	static function formatOutputContent($str,$filterHtml=true){
		$str = addslashes($str);
		$regex = '`[\r]?\n`';
		$str = preg_replace($regex, "\\\\n", $str);
		return $filterHtml==false?$str:self::filterHtml($str);		
	}
	
	/**
	 * 过滤html代码及<script>
	 * Enter description here ...
	 * @param $str
	 */
	static function filterHtml($str)
	{
  		$farr = array(
          "/<!DOCTYPE([^>]*?)>/eis",
          "/<(\/?)(html|body|head|link|meta|base|input|div|ul|li|a)([^>]*?)>/eis",
          "/<(script|i?frame|style|title|form)(.*?)<\/\\1>/eis",
          "/(< [^>]*?\s+)on[a-z]+\s*?=(\"|')([^\\2]*)\\2([^>]*?>)/isU",//过滤 javascript的on事件
          "/\s+/",//过滤多余的空白
        );
  		$tarr = array(
          "",
          "",
          "",
          "\\1\\4",
          " ",
        );
  		$str = preg_replace( $farr,$tarr,$str);
  		return $str;
	}
	
	public static function getStyle($name){
		$class = 'repaste-icon';
		switch (strtolower($name)) {
			case 'cite':
				$class = 'repaste-icon';break;
			case 'friends':
			case 'friends|add':
			case 'friends|bind':
				$class = 'msg-icon msg-friends'; break;
			case 'quest':
				$class = 'mission-icon';break;
			case 'gift':
				$class = 'gift-icon';break;
			case 'buff':
				$class = 'skill-icon';break;
			default:
				break;
		}
		return $class;
	}
	
}

//$str = '<IMG class=face src="http://i.178.com/images/smiles/em1.gif" border=0>';
//echo sms_util_text::filter_html($str);
